package io.github.harmonly.controller.api;

import io.github.harmonly.entity.activity.Activity;
import io.github.harmonly.entity.activity.ActivityComment;
import io.github.harmonly.service.ActivityService;
import io.github.harmonly.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/api")
public class ActivityController {

    @Resource
    private ActivityService service;

    @ResponseBody
    @RequestMapping(value = "/activities", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getActivities() {
        return JSONUtil.toJSONString(service.getActivities());
    }

    @ResponseBody
    @RequestMapping(value = "/activity/{cid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getActivity(@PathVariable("cid") int cid) {
        return JSONUtil.toJSONString(service.getActivity(cid));
    }

    @ResponseBody
    @RequestMapping(value = "/activity", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addActivity(@RequestBody String json) {
        return JSONUtil.result(service.addActivity(JSONUtil.parseObject(json, Activity.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/activity/{cid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteActivity(@PathVariable("cid") int cid) {
        return JSONUtil.result(service.deleteActivity(cid));
    }

    @ResponseBody
    @RequestMapping(value = "/activity", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editActivity(@RequestBody String json) {
        return JSONUtil.result(service.editActivity(JSONUtil.parseObject(json, Activity.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/activitiy_comments", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getComments() {
        return JSONUtil.toJSONString(service.getActivities());
    }

    @ResponseBody
    @RequestMapping(value = "/activity_comment/{ccid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getComment(@PathVariable("ccid") int ccid) {
        return JSONUtil.toJSONString(service.getComment(ccid));
    }

    @ResponseBody
    @RequestMapping(value = "/activity_comment", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addComment(@RequestBody String json) {
        return JSONUtil.result(service.addComment(JSONUtil.parseObject(json, ActivityComment.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/activity_comment/{ccid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteComment(@PathVariable("ccid") int ccid) {
        return JSONUtil.result(service.deleteComment(ccid));
    }

    @ResponseBody
    @RequestMapping(value = "/activity_comment", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editComment(@RequestBody String json) {
        return JSONUtil.result(service.editComment(JSONUtil.parseObject(json, ActivityComment.class)));
    }
}
